#define _CRT_SECURE_NO_WARNINGS 1
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include<math.h>
#include<stdlib.h>
#include<stdio.h>
#include<unordered_map>
#include<queue>
#include<stack>
#include <functional>
#include <memory>
using namespace std;

class Solution {
public:
    string alienOrder(vector<string>& words) {
        unordered_map<char, vector<char>> map;
        vector<int> in(30, -1);
        for (auto& ch : words)
        {
            for (auto& e : ch)
            {
                in[e - 'a'] = 0;
            }
        }
        for (int i = 0; i < words.size() - 1; i++)
        {
            int len = min(words[i].size(), words[i + 1].size());
            int flag = 1;
            for (int j = 0; j < len; j++)
            {
                if (words[i][j] != words[i + 1][j])
                {
                    map[words[i][j]].push_back(words[i + 1][j]);//t->f
                    flag = 0;
                    break;
                }
            }
            if (words[i].size() > words[i + 1].size() && flag) return "";
        }
        queue<char> qt;
        for (int i = 0; i < map.size(); i++)
        {
            for (auto& e : map[i])
            {
                in[e - 'a']++;
            }
        }
        for (int i = 0; i < in.size(); i++)
            if (in[i] == 0) qt.push(i + 'a');
        string ret;
        while (qt.size())
        {
            char tmp = qt.front();
            qt.pop();
            ret += tmp;
            for (auto& ch : map[tmp])
            {
                in[ch - 'a']--;
                if (in[ch - 'a'] == 0) qt.push(ch);
            }
        }
        for (int i = 0; i < in.size(); i++)
            if (in[i] > 0) return "";
        return ret;
    }
};

int main()
{
    vector<int> in(30, -1);
	return 0;
}